/**
 * <b> 文件: </b> Edit.java
 * @author xhe
 * @version V1.0.0.0
 * @version --2020-10-27
 */
package xhe.lib.data;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

import xhe.entities.ApiEntity;
import xhe.enums.ExecuteEnum;
import xhe.enums.OperateEnum;
import xhe.lib.base.Common;
import xhe.lib.db.BaseFactory;

/**
 * <b> 类: </b> Edit.java
 * 
 * @author xhe
 * @version V1.0.0.0
 * @version --2020-10-27
 */
public class Edit {

	/**
	 * <b> 接口: </b> 通用编辑接口
	 * 
	 * @author xhe
	 * @param requestEntity 接口属性类
	 * @return ApiEntity 接口属性类
	 * @version V1.0.0.0
	 * @version --2020年5月21日
	 * @version --新增方法
	 */
	public ApiEntity Edit_1_0(ApiEntity requestEntity) {

		try {
			String strParams = "";

			// 解析请求参数
			if (StringUtils.isEmpty(requestEntity.getParams())) {
				requestEntity.setCode(ExecuteEnum.RequestError);
				requestEntity.setMessage("请求参数不能为空!");
			} else {
				strParams = requestEntity.getParams();
			}

			BaseFactory oBaseFactory = new BaseFactory(requestEntity);

			// System.out.println(strParams);

			// 解析请求串(json)
			JSONObject jsonObject = JSON.parseObject(strParams);
			// 获取表名
			String strTable = jsonObject.getString("Table");

			if (strTable.contains(" ")) {
				strTable = strTable.split(" ")[0];
			}

			// 验证表名不允许为空
			if (StringUtils.isEmpty(strTable)) {
				requestEntity.setCode(ExecuteEnum.RequestError);
				requestEntity.setMessage("表名不能为空!");
			}

			Map<String, List<String>> strColumnList = Util.getColumnList(strParams);
			Map<Integer, List<String>> strFilterList = Util.getFilterList(strParams);
			// 查询条件
			// String strFilter = Util.getFilter(strParams);
			String strFilter = Util.getFilter(strFilterList);

			// 查询语句
			String strColumns = "";
			String strContent = "";

			for (String strColumn : strColumnList.keySet()) {
				String strValue = strColumnList.get(strColumn).get(0);
				String strType = strColumnList.get(strColumn).get(1);
				strContent += strColumn + ":" + strValue + ";";
				// String strFormat = strColumnList.get(strColumn).get(2);
				String strDefault = strColumnList.get(strColumn).get(3);

				if (strType.equals("D")) {
					if (StringUtils.isEmpty(strValue)) {
						if (StringUtils.isEmpty(strDefault)) {
							strColumns += strColumn + "=null,";
						} else {
							strColumns += strColumn + "=" + strDefault + ",";
						}
					} else {
						strColumns += strColumn + "='" + strValue + "',";
					}
				} else if (strType.equals("B")) {
					if (StringUtils.isEmpty(strValue)) {
						if (StringUtils.isEmpty(strDefault)) {
							strColumns += strColumn + "=false,";
						} else {
							strColumns += strColumn + "=" + strDefault + ",";
						}
					} else {
						strColumns += strColumn + "=" + Common.toPraseBoolean(strValue, false) + ",";
					}
				} else if (strType.equals("F") || strType.equals("I")) {
					if (StringUtils.isEmpty(strValue)) {
						if (StringUtils.isEmpty(strDefault)) {
							strColumns += strColumn + "=null,";
						} else {
							strColumns += strColumn + "=" + strDefault + ",";
						}
					} else {
						strColumns += strColumn + "=" + strValue + ",";
					}
				} else {

					if (StringUtils.isEmpty(strDefault)) {
						strColumns += strColumn + "='" + strValue + "',";
					} else {
						strColumns += strColumn + "='" + strDefault + "',";
					}
				}
			}

			if (strColumns != "") {
				strColumns = strColumns.substring(0, strColumns.length() - 1);
			}

			String strSql = "update " + strTable + " set " + strColumns + " where 1=1 " + strFilter;

			System.out.println("SQL:" + strSql);

			// 执行语句
			int iResult = oBaseFactory.Execute(strSql, requestEntity);

			// 返回结果
			Boolean blResult = false;
			String strResult = "";

			// 封装返回对象
			if (iResult > 0) {

				blResult = true;
			}

			// 封装返回结果
			strResult += "{";
			strResult += "\"result\": " + blResult + ",";
			strResult += "\"rows\": " + iResult + ",";
			strResult += "\"message\": \"" + requestEntity.getMessage() + "\"";
			strResult += "}";

			requestEntity.setReturnType(false);
			requestEntity.setResult(strResult);
			requestEntity.setTableName(strTable);
			requestEntity.setContent(strContent);
			requestEntity.setOperateType(OperateEnum.Edit.getCode());

			try {
				requestEntity.setPrimaryKey(strFilterList.get(0).get(0));
				requestEntity.setPrimaryValue(strFilterList.get(0).get(1));
			} catch (Exception ex) {
			}

		} catch (Exception ex) {

			requestEntity.setCode(ExecuteEnum.Error);
			requestEntity.setMessage(ex.getMessage());
		}

		return requestEntity;
	}

}
